package com.jxbeidou.system.setting.controller;

import com.jxbeidou.core.util.JsonUtil;
import com.jxbeidou.core.validate.ModifyValidateGroup;
import com.jxbeidou.core.validate.SaveValidateGroup;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.entity.R;
import com.jxbeidou.system.setting.convert.IgnoreUrlConvert;
import com.jxbeidou.system.setting.entity.IgnoreUrlDO;
import com.jxbeidou.system.setting.service.IgnoreUrlService;
import com.jxbeidou.system.setting.query.IgnoreUrlQuery;
import com.jxbeidou.system.setting.vo.IgnoreUrlVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.annotation.Resource;

import java.io.ObjectStreamClass;
import java.util.Arrays;
import java.util.List;

/**
* 允许访问URL表
*
* @author 杜晓祥 
* @since 1.0.0 2023-12-14
*/
@RestController
@RequestMapping("/sys/ignore-url")
@AllArgsConstructor
@Slf4j
@Validated
public class IgnoreUrlController {
    @Resource
    private IgnoreUrlService ignoreUrlService;

    @PostMapping("/page")
    public R<PageResult<IgnoreUrlVO>> page(@RequestBody @Valid IgnoreUrlQuery query){
        log.debug("查询URL:[{}]", JsonUtil.toJsonString(query));
        PageResult<IgnoreUrlVO> page = ignoreUrlService.page(query);
        return R.success(page);
    }

    @GetMapping("/{id}")
    public R<IgnoreUrlVO> get(@PathVariable("id") Long id){
        IgnoreUrlDO entity = ignoreUrlService.getById(id);

        return R.success(IgnoreUrlConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    public R<String> save(@RequestBody @Validated(SaveValidateGroup.class) IgnoreUrlVO vo){
        ignoreUrlService.save(vo);

        return R.saveSuccess();
    }

    @PutMapping
    public R<String> update(@RequestBody @Validated(ModifyValidateGroup.class) IgnoreUrlVO vo){
        ignoreUrlService.update(vo);

        return R.modifySuccess();
    }

    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable("id") Long id){
        ignoreUrlService.delete(Arrays.asList(id));

        return R.deleteSuccess();
    }

    @PutMapping("/delete")
    public R<String> delete(@RequestBody List<Long> idList){
        ignoreUrlService.delete(idList);

        return R.deleteSuccess();
    }
}